package cn.csc.module.pay.dal.mysql.jzborder;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.pay.controller.admin.jzborder.vo.JzbOrderCreateReqVO;
import cn.csc.module.pay.controller.admin.jzborder.vo.JzbOrderExportReqVO;
import cn.csc.module.pay.controller.admin.jzborder.vo.JzbOrderPageReqVO;
import cn.csc.module.pay.dal.dataobject.jzborder.JzbOrderDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

/**
 * 见证宝订单表(挂账) Mapper
 *
 * @author 曾铁树
 */
@Mapper
public interface JzbOrderMapper extends BaseMapperX<JzbOrderDO> {

    default PageResult<JzbOrderDO> selectPage(JzbOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<JzbOrderDO>()
                .betweenIfPresent(JzbOrderDO::getTradeDate, reqVO.getTradeDate())
                .eqIfPresent(JzbOrderDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(JzbOrderDO::getAmount, reqVO.getAmount())
                .eqIfPresent(JzbOrderDO::getPayOrderId, reqVO.getPayOrderId())
                .eqIfPresent(JzbOrderDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(JzbOrderDO::getTradeType, reqVO.getTradeType())
                .eqIfPresent(JzbOrderDO::getThirdId, reqVO.getThirdId())
                .eqIfPresent(JzbOrderDO::getJzbFreezeStatus, reqVO.getJzbFreezeStatus())
                .eqIfPresent(JzbOrderDO::getJzbStatus, reqVO.getJzbStatus())
                .eqIfPresent(JzbOrderDO::getJzbFrontLogNo, reqVO.getJzbFrontLogNo())
                .eqIfPresent(JzbOrderDO::getRemarks, reqVO.getRemarks())
                .betweenIfPresent(JzbOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(JzbOrderDO::getId));
    }

    default PageResult<JzbOrderDO> selectRepairPage(JzbOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<JzbOrderDO>()
                .betweenIfPresent(JzbOrderDO::getTradeDate, reqVO.getTradeDate())
                .eqIfPresent(JzbOrderDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(JzbOrderDO::getAmount, reqVO.getAmount())
                .eqIfPresent(JzbOrderDO::getPayOrderId, reqVO.getPayOrderId())
                .eqIfPresent(JzbOrderDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(JzbOrderDO::getTradeType, reqVO.getTradeType())
                .eqIfPresent(JzbOrderDO::getThirdId, reqVO.getThirdId())
                .eqIfPresent(JzbOrderDO::getJzbFreezeStatus, reqVO.getJzbFreezeStatus())
                .eqIfPresent(JzbOrderDO::getJzbStatus, reqVO.getJzbStatus())
                .eqIfPresent(JzbOrderDO::getJzbFrontLogNo, reqVO.getJzbFrontLogNo())
                // remarks字段设置为1表示已经修复
                .neIfPresent(JzbOrderDO::getRemarks, "1")
                .betweenIfPresent(JzbOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(JzbOrderDO::getId));
    }

    default List<JzbOrderDO> selectList(JzbOrderExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<JzbOrderDO>()
                .betweenIfPresent(JzbOrderDO::getTradeDate, reqVO.getTradeDate())
                .eqIfPresent(JzbOrderDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(JzbOrderDO::getAmount, reqVO.getAmount())
                .eqIfPresent(JzbOrderDO::getPayOrderId, reqVO.getPayOrderId())
                .eqIfPresent(JzbOrderDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(JzbOrderDO::getTradeType, reqVO.getTradeType())
                .eqIfPresent(JzbOrderDO::getThirdId, reqVO.getThirdId())
                .eqIfPresent(JzbOrderDO::getJzbFreezeStatus, reqVO.getJzbFreezeStatus())
                .eqIfPresent(JzbOrderDO::getJzbStatus, reqVO.getJzbStatus())
                .eqIfPresent(JzbOrderDO::getJzbFrontLogNo, reqVO.getJzbFrontLogNo())
                .eqIfPresent(JzbOrderDO::getRemarks, reqVO.getRemarks())
                .betweenIfPresent(JzbOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(JzbOrderDO::getId));
    }

    default JzbOrderDO selectDetail(JzbOrderCreateReqVO reqVO){
        return selectOne(new LambdaQueryWrapperX<JzbOrderDO>()
                .eqIfPresent(JzbOrderDO::getMerchantId, reqVO.getMerchantId())
                .eqIfPresent(JzbOrderDO::getAppUserId, reqVO.getAppUserId())
                .eqIfPresent(JzbOrderDO::getPayOrderId, reqVO.getPayOrderId())
                .eqIfPresent(JzbOrderDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(JzbOrderDO::getTradeType, reqVO.getTradeType())
                .eqIfPresent(JzbOrderDO::getThirdId, reqVO.getThirdId())
                .eqIfPresent(JzbOrderDO::getJzbFreezeStatus, reqVO.getJzbFreezeStatus())
                .eqIfPresent(JzbOrderDO::getJzbStatus, reqVO.getJzbStatus()));

    }

    default JzbOrderDO selectByPayOrderId(BigInteger orderId){
        return selectOne(new LambdaQueryWrapperX<JzbOrderDO>()
                .eq(JzbOrderDO::getPayOrderId, orderId)
        );

    }

    /**
     * 通过支付订单交易号查询见证宝订单
     * @param tradeNo 支付订单交易号(successExno)
     * @return
     */
    default JzbOrderDO selectByTradeNo(String tradeNo){
        return selectOne(new LambdaQueryWrapperX<JzbOrderDO>()
                .eq(JzbOrderDO::getTradeNo, tradeNo)
        );

    }

    JzbOrderDO selectByMerchantOrderId(String merchantOrderId);

    /**
     * 部分更新字段
     * @param jzbOrderDO 见证宝订单数据
     * @return
     */
    Integer updateRemarkById(JzbOrderDO jzbOrderDO);

    /**
     * 更新见证宝订单状态
     * @param id 订单ID
     * @param jzbFreezeStatus 见证宝订单状态
     * @return
     */
    default Integer updateJzbFreezeStatus(BigInteger id, Integer jzbFreezeStatus) {
        LambdaUpdateWrapper<JzbOrderDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(JzbOrderDO::getJzbFreezeStatus, jzbFreezeStatus);
        userUpdateWrapper.eq(JzbOrderDO::getId, id);
        return update(null, userUpdateWrapper);

    }

    /**
     * 更新见证宝系统相关的seq
     * @param orderDO 见证宝订单信息
     * @return
     */
    default Integer updateJzbSeq(JzbOrderDO orderDO){
        LambdaUpdateWrapper<JzbOrderDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(JzbOrderDO::getJzbFreezeStatus, orderDO.getJzbFreezeStatus());
        // 冻结充值电商见证宝系统生成的流水号, 调用KFEJZB6267接口返回的JzbAcceptSeqNo
        Optional.ofNullable(orderDO.getFreezeRechargeSeq()).ifPresent( seq -> userUpdateWrapper.set(JzbOrderDO::getFreezeRechargeSeq, seq));
        Optional.ofNullable(orderDO.getFreezeTime()).ifPresent( time -> userUpdateWrapper.set(JzbOrderDO::getFreezeTime, time));
        // 撤销冻结充值电商见证宝系统生成的流水号，调用KFEJZB6268接口返回的JzbAcceptSeqNo
        Optional.ofNullable(orderDO.getRefundFreezeSeq()).ifPresent( seq -> userUpdateWrapper.set(JzbOrderDO::getRefundFreezeSeq, seq));
        Optional.ofNullable(orderDO.getRefundFreezeTime()).ifPresent( time -> userUpdateWrapper.set(JzbOrderDO::getRefundFreezeTime, time));
        // 分账电商见证宝系统生成的流水号，调用KFEJZB6271接口返回的JzbAcceptSeqNo
        Optional.ofNullable(orderDO.getDivideSeq()).ifPresent( seq -> userUpdateWrapper.set(JzbOrderDO::getDivideSeq, seq));
        Optional.ofNullable(orderDO.getDivideTime()).ifPresent( time -> userUpdateWrapper.set(JzbOrderDO::getDivideTime, time));
        // 撤销分账电商见证宝系统生成的流水号，调用KFEJZB6272接口返回的JzbAcceptSeqNo
        Optional.ofNullable(orderDO.getRefundDivideSeq()).ifPresent( seq -> userUpdateWrapper.set(JzbOrderDO::getRefundDivideSeq, seq));
        Optional.ofNullable(orderDO.getRefundDivideTime()).ifPresent( time -> userUpdateWrapper.set(JzbOrderDO::getRefundDivideTime, time));
        userUpdateWrapper.eq(JzbOrderDO::getId, orderDO.getId());
        return update(null, userUpdateWrapper);
    }

}
